package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.ImgFeiCode;
import com.shangxian.giveithome.entity.Img;
import com.shangxian.giveithome.entity.RqCode;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.WxCodeUnLimit;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.ImgMapper;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.ImgFeiService;
import com.shangxian.giveithome.service.RqCodeService;
import com.shangxian.giveithome.utils.RedisUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@Api(tags = "图片Fei")
@Component
//@RestController
//@RequestMapping("/imgFei")
public class ImgFeiServer {


  @Autowired
  private RedisUtils redisUtils;

  @Autowired
  private ImgFeiService imgFeiService;

  @Autowired
  private RqCodeService rqCodeService;

  @Autowired
  private HttpServletRequest request;

  @Resource
  private ImgMapper imgMapper;



  /**
   * 根据收容id集合查询该集合的图片数据
   *
   * @param ids
   * @return
   */
  @ApiOperation(value = "根据收容id集合查询该集合的图片数据")
  @PostMapping("/queryAsylumImages")
  public List<Img> queryImages(@RequestParam(value = "ids") List<Long> ids) {
    return imgFeiService.queryAsylumImgs(ids);
  }

  /**
   * 根据img id集合查询图片路径
   *
   * @param imgIds
   * @return
   */
//  public List<Img> queryImagesByIds(List<Long> imgIds) {
//    return imgFeiService.queryByIds(imgIds);
//  }
  @ApiOperation(value = "根据img id集合查询图片路径")
  @PostMapping("/queryByIds")
  public List<Img> queryImagesByIds(@RequestParam(value = "imgIds") List<Long> imgIds) {
    return imgFeiService.queryByIds(imgIds);
  }


  /**
   * 查看二维码数据
   *
   * @param ac_id
   * @return
   */
//  public RqCode queryRq(Long ac_id) {
//    UserDetails header = redisUtils.getHeader(request);
//    return rqCodeService.queryRqCode(header.getId(), ac_id);
//  }
  @ApiOperation(value = "查看二维码数据")
  @GetMapping("/queryRqCode/{ac_id}")
  public RqCode queryRq(@PathVariable Long ac_id) {
    UserDetails header = redisUtils.getHeader(request);
    return rqCodeService.queryRqCode(header.getId(), ac_id);
  }

  /**
   * 查看图片集合根据
   *
   * @param imgId
   * @return
   */
//  public List<Img> queryImagesByImgs(Long imgId) {
//    return imgFeiService.queryByImages(imgId);
//  }
  @ApiOperation(value = "查看图片集合根据")
  @GetMapping("/queryImages/{imgId}")
  public List<Img> queryImagesByImgs(@PathVariable Long imgId) {
    return imgFeiService.queryByImages(imgId);
  }


  /**
   * 查看活动二维码
   *
   * @param acId
   * @return
   */
//  public RqCode queryRqCode(Long acId) {
//    return imgFeiService.queryRecode(acId);
//  }
  @ApiOperation(value = "查看活动二维码")
  @GetMapping("/queryRqCodeActivity/{acId}")
  public RqCode queryRqCode(@PathVariable Long acId) {
    return imgFeiService.queryRecode(acId);
  }


  /**
   * 查看机构二维码
   *
   * @param meId
   * @return
   */
//  public RqCode queryRqCodeMe(Long meId) {
//    return imgFeiService.queryRecodeMe(meId);
//  }
  @ApiOperation(value = "查看机构二维码")
  @GetMapping("/queryRqCodeMechanism/{meId}")
  public RqCode queryRqCodeMe(@PathVariable Long meId) {
    return imgFeiService.queryRecodeMe(meId);
  }

  /**
   * 保存机构二维码
   *
   * @param rqCode
   * @return
   */
  public String saveRqCodeMe(RqCode rqCode) {
    return imgFeiService.saveRqcodeMe(rqCode);
  }
//  @ApiOperation(value = "保存机构二维码")
//  @PostMapping("/saveRqCodeMe")
//  public String saveRqCodeMe(@RequestBody RqCode rqCode){
//    return imgFeiService.saveRqcodeMe(rqCode);
//  }

  /**
   * 保存活动二维码
   *
   * @param rqCode
   * @return
   */
  public String saveRqCode(RqCode rqCode) {
    return imgFeiService.saveRqcode(rqCode);
  }
//  @ApiOperation(value = "保存活动二维码")
//  @PostMapping("/saveRqCode")
//  public String saveRqCode(@RequestBody RqCode rqCode) {
//    return imgFeiService.saveRqcode(rqCode);
//  }

  /**
   * 根据活动id删除二维码
   *
   * @param id
   */
  public Integer deleteRqCode(Long id) {
    return imgFeiService.deleteRq(id);
  }
//  @ApiOperation(value = "根据活动id删除二维码")
//  @DeleteMapping("/deleteRq")
//  @CheckLoginStatus
//  public Integer deleteRqCode(Long id) {
//    return imgFeiService.deleteRq(id);
//  }

  public void delete(String path) {
    if (StringUtils.isEmpty(path)) {
      ExceptionUtils.error(CommonCode.DATA_ERRORS);
    }
    imgFeiService.deletePath(path);
  }
//  @ApiOperation(value = "根据图片路径删除图片文件")
//  @DeleteMapping(value = "/delete", name = "根据图片路径删除图片文件")
//  @CheckLoginStatus
//  public void delete(String path) {
//    if (StringUtils.isEmpty(path)) {
//      ExceptionUtils.error(CommonCode.DATA_ERRORS);
//    }
//    imgFeiService.deletePath(path);
//  }

  public Result deleteKeyId(Long me_id, Long key_id) {

    List<Img> imgs = imgMapper.queryByKeyId(key_id);
    if (CollectionUtils.isEmpty(imgs)) {
      return Result.error(CommonCode.DATA_ERROR);
    }
    for (Img img : imgs) {
      if (!img.getKeyId().equals(key_id)) {
        return Result.error(ImgFeiCode.KEY_ID_NOT_PRE);
      }
    }
    return imgFeiService.deleteMe(me_id, imgs);
  }
//  @ApiOperation(value = "根据key_id删除图片文件")
//  @DeleteMapping(value = "/deleteKeyId", name = "根据key_id删除图片文件")
//  @CheckLoginStatus
//  public Result deleteKeyId(@RequestParam("me_id") Long me_id, @RequestParam("key_id") Long key_id) {
//
//    List<Img> imgs = imgMapper.queryByKeyId(key_id);
//    if (CollectionUtils.isEmpty(imgs)) {
//      return Result.error(CommonCode.DATA_ERROR);
//    }
//    for (Img img : imgs) {
//      if (!img.getKeyId().equals(key_id)) {
//        return Result.error(ImgFeiCode.KEY_ID_NOT_PRE);
//      }
//    }
//    return imgFeiService.deleteMe(me_id, imgs);
//  }

  // 获取宠物二维码图片
  public String queryPetQrCode(WxCodeUnLimit wxCodeUnLimit) {
      return imgFeiService.queryPetQrCode(wxCodeUnLimit);
  }
}
